<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!Doctype html>
<html>
<head>
    <meta content="text/html" charset="utf-8">
    <title>党建</title>
    <meta name="keywords" content="务川,党建"/>
    <meta name="description" content="务川,党建"/>
    <link href="../../static/images/ico/favicon.png" rel="shortcut icon">
    <link rel="stylesheet" type="text/css" href="../../static/css/test.css">
    <script type="text/javascript" src="../../static/js/user/jq181.js"></script>

    <script type="text/javascript">
        var testNextUrl = "/exam/examTestNext.do";
        var restSecond = ${time} * 60 - 1;
        //倒计时结束后进行提交
        var callBackFun = function(){
            $(window).unbind('beforeunload');
            var nameList = checkInput(false);
            $("#nameList").val(nameList);
            var data = $("#questionFrame").serializeArray();
            $.ajax({
                type: "POST",
                url: testNextUrl,
                data: data,
                success: function(data) {
                    alert("时间到，考试结束");
                    location.href = "/test/testList.do";
                },
                error:function(){
                    alert("时间到，考试结束");
                    location.href = "/test/testList.do";
                }
            });

        }
        //关闭前提示
        $(window).bind('beforeunload',function(){
            return "确认结束考试吗？";
        });
        $(function(){
            //绑定下一页按钮响应事件
           $("#test_next").click(function(){
               var nameList = checkInput(true);
               if(nameList == "")
               {
                   alert("还有题目未选择答案，请选择。")
                   return;
               }
               var ok = window.confirm("本页的答案将不能修改，确认进入下一页？");
                if(ok)
                {
                    $("#nameList").val(nameList);
                    var data = $("#questionFrame").serializeArray();
                    $.ajax({
                        type: "POST",
                        url: testNextUrl,
                        data: data,
                        success: function(data){
                            var isFinish = data.isFinish;
                            if(isFinish == "1")
                            {
                                alert("所有题目都已经做完，考试结束");
                                location.href = "/test/testList.do";
                            }
                            else
                            {
                                $("#preNum").html(data.preMin+"-"+data.preMax);
                                var testList = data.testList;
                                if(testList && testList.length >0)
                                {
                                    var content = "";
                                    for(var i=0;i<testList.length; i++)
                                    {
                                        var test = testList[i];
                                        content = content +"<li>";
                                        content = content +"<p>"+(i+1)+"、"+test.title+"</p>";
                                        content = content +"<div>";
                                        var type = "radio";
                                        if(test.issingle == "0")
                                        {
                                            type = "checkbox";
                                        }
                                        var answerList = test.answerList;
                                        for(var k=0; k<answerList.length;k++)
                                        {
                                            var answer = answerList[k];
                                            content = content +"<label><input name='"+test.id+"' value='"+answer.id+"' type='"+type+"'>" +
                                                    "<span>"+answer.content+"</span></label>";
                                        }
                                        content = content +"</div>";
                                        content = content +"</li>";
                                    }
                                    $("#questionFrame ul").html("");
                                    $("#questionFrame ul").html(content);
                                }
                            }
                        }
                    });
                }
           });
            //绑定交卷按钮
            $("#test_submit").click(function(){
                var is = confirm("确认交卷？");
                if(is)
                {
                    $(window).unbind('beforeunload');
                    var nameList = checkInput(false);
                    $("#nameList").val(nameList);
                    var data = $("#questionFrame").serializeArray();
                    $.ajax({
                        type: "POST",
                        url: testNextUrl,
                        data: data,
                        success: function(data) {
                            location.href = "/test/testList.do";
                        },
                        error:function(){
                            location.href = "/test/testList.do";
                        }
                    });

                }
            });
        });

        //check true,检查有没有没有选择的，false不检查
        function checkInput(check)
        {
            var liList = $("#questionFrame ul li");
            var nameList = "";
            for(var i=0;i<liList.length;i++)
            {
                var input = $(liList[i]).find("input:eq(0)");
                var name = $(input).attr("name");
                var type = $(input).attr("type");
                var num =  $("#questionFrame").find("input[type='"+type+"'][name='"+name+"']:checked").length;
                //还有题目没有选择答案
                if(num <= 0 && check)
                {
                    return "";
                }
                if(i==0)
                {
                    nameList = name;
                }
                else
                {
                    nameList = nameList +","+name;
                }
            }
            return nameList;
        }
    </script>
    <script type="text/javascript" src="../../static/js/user/test.js"></script>
</head>
<body>
<jsp:include page="../../themes/header.jsp"/>
<div id="middle">
    <div class="wrapper">
        <div id="middle-left-aside">
            <div class="left-nav-column column">
                <div class="column-header">&gt;&gt;<span>信息</span></div>
                <div class="column-body">
                    <ul class="left-test-panel">
                        <li class="info">
                            <ul>
                                <li><span>姓名：</span>${realName}</li>
                                <li><span>账号：</span>${passport}</li>
                            </ul>
                        </li>
                        <li class="time">
                            <div id="left-test-time-wrapper">
                                <span></span>&nbsp;<div></div>
                            </div>
                        </li>
                        <li  style="margin-top: 20px;"><button id="test_submit" class="btn2">交卷</button></li>
                    </ul>
                </div>
                <div class="column-bottom"></div>
            </div>
        </div>
        <div id="middle-right-content">
            <div class="wrapper2">
                <div class="content-header">
                    <div>
                        <%--<span></span>当前位置：<a href="/">首页</a> &gt; <a href="/test/testList.do">在线考试</a>--%>
                        <label>题目总数:</label>${num}<label>当前：</label><label id="preNum">${preMin}-${preMax}</label>
                    </div>
                    <span>在线考试</span>
                </div>
                <form id="questionFrame" >

                    <input type="hidden" name="nameList" id="nameList" value="" />
                    <input type="hidden" name="id" value="${id}" />
                    <input type="hidden" name="preScore" value="${preScore}" />
                    <ul>
                        <c:forEach var="item" items="${testList}" varStatus="status">
                            <li>
                                <p>${(status.index+1)}、${item.title}</p>
                                <div>
                                    <c:if test="${item.issingle == '1'}">
                                        <c:forEach var="an" items="${item.answerList}" varStatus="status">
                                            <label><input name="${item.id}" value="${an.id}" type="radio"><span>${an.content}</span></label>
                                        </c:forEach>
                                    </c:if>
                                    <c:if test="${item.issingle == '0'}">
                                        <c:forEach var="an" items="${item.answerList}" varStatus="status">
                                            <label><input name="${item.id}" value="${an.id}" type="checkbox"><span>${an.content}</span></label>
                                        </c:forEach>
                                    </c:if>
                                </div>
                            </li>
                        </c:forEach>
                    </ul>
                </form>

                <ul class="left-test-panel">
                    <li style="float: left;display: inline;"><button id="test_next" class="btn">下一页</button></li>
                </ul>
            </div>
        </div>
    </div>
</div>
<jsp:include page="../../themes/footer.jsp"/>
</body>
</html>